package com.xquant.databasemanagement.controller.windows;

import com.xquant.databasemanagement.bean.table.ColumnBean;
import com.xquant.databasemanagement.controller.FormController;
import com.xquant.databasemanagement.enums.ColumnType;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.*;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

import java.net.URL;
import java.util.ResourceBundle;

public class ColumnWindowController implements Initializable, FormController {


    public CheckBox nullable;
    public TextField columnDefault;
    @FXML
    private VBox columnWindow;

    @FXML
    private TextField columnName;
    @FXML
    private TextField columnLength;
    @FXML
    private TextField columnPrecision;
    @FXML
    private TextField columnAnnotation;
    @FXML
    private ComboBox<ColumnType> columnType;


    private Stage parentWin;

    ColumnBean columnBean = null;



    @Override
    public void initialize(URL location, ResourceBundle resources) {
        columnType.getItems().addAll(ColumnType.values());
        columnType.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<ColumnType>() {
            @Override
            public void changed(ObservableValue<? extends ColumnType> observable, ColumnType oldValue, ColumnType newValue) {
                switch (newValue){
                    case BLOB:
                    case CLOB:
                        columnLength.setEditable(false);
                        columnPrecision.setEditable(false);
                        break;
                    case NUMBER:
                        columnLength.setEditable(true);
                        columnPrecision.setEditable(true);
                        break;
                    case VARCHAR:
                    case NVARCHAR:
                    case CHAR:
                    case TIMESTAMP:
                    case INT:
                        columnLength.setEditable(true);
                        columnPrecision.setEditable(false);
                        break;


                }
            }
        });
    }




    @FXML
    private void actionSaveButton(ActionEvent event) {
        if (isValid()) {
            parentWin = (Stage) columnWindow.getScene().getWindow();
            if (columnBean == null) {
                setColumnBean();
                TableView view = ((TableView) parentWin.getOwner().getScene().lookup("#columnTable"));
                view.getItems().addAll(this.columnBean);
            }else {
                setColumnBean();
            }
            clear();
            parentWin.close();
        }
    }





    public void setColumnBean(ColumnBean column){
        this.columnBean = column;
        columnName.setText(column.getColumnName());
        columnAnnotation.setText(column.getColumnAnnotation());
        columnLength.setText(column.getColumnLength());
        columnType.getSelectionModel().select(ColumnType.valueOf(column.getColumnType()));
        columnPrecision.setText(column.getColumnPrecision());
        nullable.setSelected(column.getNullable());
        columnDefault.setText(column.getColumnDefault());
    }


    private void setColumnBean() {
        if (this.columnBean == null)
            this.columnBean = new ColumnBean();
        columnBean.setColumnName(columnName.getText());
        columnBean.setColumnLength(columnLength.getText());
        columnBean.setColumnPrecision(columnPrecision.getText());
        columnBean.setColumnType(columnType.getValue().toString());
        columnBean.setColumnAnnotation(columnAnnotation.getText());
        columnBean.setNullable(nullable.isSelected()+"");
        columnBean.setColumnDefault(columnDefault.getText());
    }


    @Override
    public void clear(){
        columnBean = null;
        columnName.clear();
        columnLength.clear();
        columnPrecision.clear();
        columnAnnotation.clear();
        columnDefault.clear();
    }

    @Override
    public boolean isValid() {
        if (!columnName.getText().matches("^\\w+$")){
            new Alert(Alert.AlertType.NONE, "字段命名不正确", new ButtonType[]{ButtonType.CLOSE}).show();
            return false;
        }else if (columnType.getSelectionModel().getSelectedItem()==null){
            new Alert(Alert.AlertType.NONE, "请选择字段类型", new ButtonType[]{ButtonType.CLOSE}).show();
            return false;
        }else
            return true;
    }

}
